package com.ordersystem.controller;

import com.ordersystem.domain.Dish;
import com.ordersystem.domain.DishRank;
import com.ordersystem.domain.Result;
import com.ordersystem.service.DishService;
import com.ordersystem.utils.PageUtils;
import com.ordersystem.utils.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : DishController
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/26 13:52]
 * @updateUser : asus1020
 * @updateTime : [2022/3/26 13:52]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private UploadFileUtils uploadFileUtils;
    PageUtils pageUtils=new PageUtils();
    @PostMapping("/addDish")//添加菜品
    public Result addDish(@RequestBody Dish dish){
        System.out.println(dish+"-----添加菜品表单");
        Result result = new Result();
        int total = dishService.addDish(dish);
        if (total>0){
            result.setStatus(200);
            Dish dishById = dishService.findDishById(dish.getDid());
            result.setMessage("添加菜品成功");
            result.setData(dishById);
        }else {
            result.setStatus(500);
            result.setMessage("添加菜品失败");
        }
        return result;
    }
    @DeleteMapping("/deleteDish/{did}")//删除菜品信息
    public Result deleteDish(@PathVariable("did") Integer did){
        Result result = new Result();
        int total = dishService.deleteDish(did);
        if (total>0){
            result.setStatus(200);
            result.setMessage("菜品信息删除成功");
        }else {
            result.setStatus(500);
            result.setMessage("菜品信息删除失败");
        }
        return result;
    }
    @PutMapping("/updateDish")//修改菜品
    public Result updateDish(@RequestBody Dish dish){
        Result result = new Result();
        int total = dishService.updateDish(dish);
        if (total>0){
            result.setStatus(200);
            result.setMessage("修改菜品成功");
            result.setData(dishService.findDishById(dish.getDid()));
        }else {
            result.setStatus(500);
            result.setMessage("修改菜品失败");
        }
        return result;
    }
    @GetMapping("/findDishes")//根据条件查询菜品
    public Result findDishes(String query, Integer cid, Integer pagenum, Integer pagesize){
        Result result = new Result();
        Map<String, Object> dishes = dishService.findDishes(query, cid, pagenum, pagesize);
        if (dishes!=null){
            result.setStatus(200);
            result.setMessage("菜品查询成功");
            result.setData(dishes);
        }else {
            result.setStatus(500);
            result.setMessage("菜品查询失败");
        }
        return result;
    }
    @GetMapping("/findDishById/{did}")//根据id查询单个菜品信息
    public Result findDishById(@PathVariable("did") Integer did){
        Result result = new Result();
        Dish dish = dishService.findDishById(did);
        if (dish!=null){
            result.setStatus(200);
            result.setMessage("查询菜品成功");
            result.setData(dish);
        }else {
            result.setStatus(500);
            result.setMessage("查询菜品信息失败");
        }
        return result;
    }
    @GetMapping("/findShoppingDish/{uid}")//查询所有购物车商品
    public Result findShoppingDish(@PathVariable("uid") Integer uid){
        Result result = new Result();
        List<Dish> shoppingDish = dishService.findShoppingDish(uid);
        if (shoppingDish!=null){
            result.setStatus(200);
            result.setMessage("查询购物车成功");
            result.setData(shoppingDish);
        }else {
            result.setStatus(500);
            result.setMessage("查询购物车失败");
        }
        return result;
    }
    @GetMapping("/findOrderDish")
    public Result findOrderDish(String dids){
        Result result = new Result();
        List<Dish> orderDish = dishService.findOrderDish(dids);
        if (orderDish!=null){
            result.setStatus(200);
            result.setMessage("查询订单菜品成功");
            result.setData(orderDish);
        }else {
            result.setStatus(500);
            result.setMessage("查询订单菜品失败");
        }
        return result;
    }
    @GetMapping("/findOrderDishByUidAndDids")
    public Result findOrderDishByUidAndDids(String dids, Integer uid){
        Result result = new Result();
        List<Dish> dishByUidAndDids = dishService.findOrderDishByUidAndDids(uid, dids);
        if (dishByUidAndDids!=null){
            result.setStatus(200);
            result.setMessage("查询订单信息成功");
            result.setData(dishByUidAndDids);
        }else {
            result.setStatus(500);
            result.setMessage("查询订单菜品失败");
            result.setData(null);
        }


        return result;
    }
    @GetMapping("/addShoppingTrolley")//添加购物车
    public Result addShoppingTrolley(Integer uid, Integer did, Integer number){
        Result result = new Result();
        if (uid!=null&&did!=null&&number!=null){
            int total = dishService.addShoppingTrolley(uid, did, number);
            if (total>0){
                result.setStatus(200);
                result.setMessage("添加购物车成功");
            }else {
                result.setStatus(500);
                result.setMessage("添加购物车失败");
            }
        }else {
            result.setStatus(500);
            result.setMessage("你的参数有误，请检查");
        }

        return result;
    }
    @GetMapping("/findDishesByCid")
    public Result findDishesByCid(Integer cid, Integer uid){
        Result result = new Result();
        List<Dish> dishesByCid = dishService.findDishesByCid(cid,uid);
        if (dishesByCid!=null){
            result.setStatus(200);
            result.setMessage("查询菜品成功");
            result.setData(dishesByCid);
        }else {
            result.setStatus(500);
            result.setMessage("查询菜品失败");
        }
        return result;
    }
    @DeleteMapping("/deleteShoppingTrolley/{uid}/{did}")
    public Result deleteShoppingTrolley(@PathVariable("uid") Integer uid, @PathVariable("did") Integer did){
        Result result = new Result();
        int total = dishService.deleteShopping(uid, did);
        if (total>0){
            result.setStatus(200);
            result.setMessage("删除购物车成功");
        }else {
            result.setStatus(500);
            result.setMessage("删除购物车失败");
        }

        return result;
    }
    @GetMapping("/getDishRank/{rank}")
    public Result getDishRank(@PathVariable("rank") Integer rank){
        Result result = new Result();
        List<DishRank> dishRankList = dishService.getDishRank(rank);
        if (dishRankList!=null){
            result.setStatus(200);
            result.setData(dishRankList);
        }else {
            result.setStatus(500);
            result.setMessage("获取数据失败");
        }

        return result;
    }
    //根据订单id信息查询订单详情菜品信息
    @GetMapping("/findDishByOid/{oid}")
    public Result findDishByOid(@PathVariable("oid") Integer oid){
        List<Dish> dishDetailByOid = dishService.findDishDetailByOid(oid);
        if (dishDetailByOid!=null){
           return Result.success(dishDetailByOid);
        }
        return Result.fail("未查询到订单信息");
    }
}
